import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        token: '',
        userInfo: null

    },
    getters: {
        getToken(state) {
            return state.token;
        },
        getUserInfo(state) {
            if (!state.userInfo) {
                state.userInfo = JSON.parse(localStorage.getItem('user'));
            } else {
                return state.userInfo;
            }
        }
    },
    mutations: {
        setToken(state, token) {
            state.token = token;
            localStorage.setItem('token', token);
        },
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo;
            localStorage.setItem('user', JSON.stringify(userInfo));
        },
        //退出登录时要清楚token和用户信息
        clearToken(state) {
            state.token = '';
            state.userInfo = null;
            localStorage.removeItem('token');
            localStorage.removeItem('user');
        }

    },
    actions: {
        // ???action呢
    }
})